package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.LimitCouponConfig;
import com.rm.zeyou.entity.LimitCouponMatchesConfig;
import com.rm.zeyou.entity.UserCoupon;
import com.rm.zeyou.mapper.LimitCouponConfigMapper;
import com.rm.zeyou.mapper.LimitCouponMatchesConfigMapper;
import com.rm.zeyou.mapper.UserCouponMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.LimitCouponConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.response.SelectLimitCouponListResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 限时优惠券表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:08
 */
@Service
public class LimitCouponConfigServiceImpl extends ServiceImpl<LimitCouponConfigMapper, LimitCouponConfig> implements LimitCouponConfigService {
    @Resource
    private LimitCouponMatchesConfigMapper limitCouponMatchesConfigMapper;
    @Resource
    private LimitCouponConfigMapper limitCouponConfigMapper;
    @Resource
    private UserCouponMapper userCouponMapper;

    @Override
    public ApiRestResponse<List<SelectLimitCouponListResponseVo>> selectLimitCouponList(Long userId, Long id) {
        List<LimitCouponConfig> limitCouponConfigs = limitCouponConfigMapper.selectList(new LambdaQueryWrapper<LimitCouponConfig>()
                .eq(LimitCouponConfig::getMatchesId, id));

        List<SelectLimitCouponListResponseVo> voList = new ArrayList<>();
        if (!CheckEmptyUtil.isEmpty(limitCouponConfigs)) {
            LimitCouponMatchesConfig limitCouponMatchesConfig = limitCouponMatchesConfigMapper.selectById(id);
            limitCouponConfigs.forEach(item -> {
                SelectLimitCouponListResponseVo vo = new SelectLimitCouponListResponseVo();
                vo.setId(item.getId());
                vo.setCouponFull(item.getCouponFull());
                vo.setCouponSubtract(item.getCouponSubtract());
                if (limitCouponMatchesConfig.getLimitStatus().equals(NumberConstants.ZERO_CONSTANTS)) {
                    //即将开场
                    vo.setCouponStatus(NumberConstants.ZERO_CONSTANTS);
                } else if (limitCouponMatchesConfig.getLimitStatus().equals(NumberConstants.TWO_CONSTANTS)) {
                    //已结束
                    vo.setCouponStatus(NumberConstants.TWO_CONSTANTS);
                } else {
                    //正在抢
                    //查询用户已领此优惠券的次数
                    int limitCouponCount = userCouponMapper.selectCount(new LambdaQueryWrapper<UserCoupon>()
                            .eq(UserCoupon::getCouponId, item.getId())
                            .eq(UserCoupon::getUserId, userId)
                            .eq(UserCoupon::getCouponType, NumberConstants.ONE_CONSTANTS)).intValue();
                    if (limitCouponCount < item.getCouponLimitReceiveTotal()) {
                        //如果用户已有优惠券数量 < 限时优惠券可领次数 表示还可以在领取
                        vo.setCouponStatus(NumberConstants.ONE_CONSTANTS);
                    } else {
                        //如果用户已有优惠券数量 >= 限时优惠券可领次数 表示不可再领取
                        vo.setCouponStatus(NumberConstants.TWO_CONSTANTS);
                    }
                }

                voList.add(vo);
            });
        }
        return ApiRestResponse.success(voList);
    }
}
